package game;

import java.awt.Color;
import java.awt.Graphics;

public class Brick{
	
	public Color color;
	private int x, y, width, height, numHits, maxHits;
	private boolean isVisible;
	public Pill pill;
	
	
	public Brick(int x, int y, int width, int height, int pillType, boolean metal){
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;

		pill = new Pill(x+(width/4), y+(height/4), width/2, height/2, pillType);
		if(metal){
			maxHits = 2;
			this.color = Color.RED;
		}else{
			maxHits = 1;
			this.color = Color.CYAN;
		}
		numHits = 0;
		this.isVisible = true;
	}
	public void setX(int x){
		this.x = x;
	}

	public void addHits(){
		numHits++;
		color = Color.CYAN;
		if(numHits == maxHits){
			isVisible = false;
		}
	}
	public boolean hitBottom(int X, int Y){
		if(( X >= x ) && ( X <= x + width ) && (( y + height == Y )||( y + height == Y + 1 )) && ( isVisible == true)){
			addHits();
			return true;
		}
		return false;
	}
	
	public void setVisible(boolean b){
		isVisible = b;
	}
	
	public boolean hitTop(int X, int Y){
		if(( X >= x ) && (X <= x + width) && (( y == Y )||( y == Y - 1 )) && ( isVisible == true )){
			addHits();
			return true;
		}
		return false;
	}
	
	public boolean hitLeft(int X, int Y){
		if(( Y >= y ) && ( Y <= y + height ) && (( x == X )||( x == X - 1 )) && ( isVisible == true )){
			addHits();
			return true;
		}
		return false;
	}
	
	public boolean hitRight(int X, int Y){
		if(( Y >= y ) && ( Y <= y + height ) && (( x + width == X )||( x + width == X - 1 )) && ( isVisible == true )){
			addHits();
			return true;
		}
		return false;
	}
	
	public boolean destroyed(){
		return !isVisible;
	}
	
	public void draw(Graphics g){
		if(isVisible){
			Color oldColor = g.getColor();
			g.setColor(color);
			g.fillRect(x,y,width,height);
			g.setColor(oldColor);
		}
	}
	
}
